﻿layui.use(['comment','form'], function () {
    var comment = layui.comment,
        form = layui.form,
        $ = layui.$;

    /**
     * 获取用户信息
     * */
    comment._ACP(ApiUrl.LanbtUser.UserInfo, {}, function (res) {
        if (res.success) {
            var data = res.result;
            $('#headUrl').attr('src', data.headUrl);
            $('#userName').html(data.userName);
            $('#password').html(data.password);
            form.val('lanbt-user-form', data);
        } else {
            comment.Error(res.errorMsg);
        }
    });

    /**
     * 用户信息保存
     * */
    form.on('submit(save)', function (data) {
        var field = data.field;
        field['headUrl'] = $('#headUrl').attr('src');
        comment._ACP(ApiUrl.LanbtUser.userUpdate, JSON.stringify(field), function (res) {
            if (res.success) {
                comment.Success(res.result);
            } else {
                comment.Error(res.errorMsg);
            }
        })
    })

    /**
     * 更改密码
     * */
    form.on('submit(change-password)', function () {
        $('#lanbt-user-info').addClass('layui-hide');
        $('#lanbt-user-password').removeClass('layui-hide');
        form.val("lanbt-password-form", {
            "oldPassword": '',
            "password": '',
            "confirmPassword":''
            });
    })

    /**
     * 保存密码
     * */
    form.on('submit(save-password)', function (data) {
        var field = data.field;
        field["id"] = comment.currentUser.id;
        comment._ACP(ApiUrl.LanbtUser.userPasswordUpdate, JSON.stringify(field), function (res) {
            if (res.success) {
                comment._ACP(ApiUrl.Home.signOut, {}, function (data) {
                    if (res.success) {
                        layer.msg(res.result + '请重新登录~');
                        setTimeout(function () {
                            window.localStorage.removeItem("token");
                            window.localStorage.removeItem("currentUser");
                            window.parent.location.href = login;
                        }, 1000)
                    } else {
                        comment.Error(res.errorMsg);
                    }
                });
            } else {
                comment.Error(res.errorMsg);
            }
        })
    })

    /**
     * 密码界面返回
     * */
    $('#back').click(function () {
        $('#lanbt-user-info').removeClass('layui-hide');
        $('#lanbt-user-password').addClass('layui-hide');
    })

    form.verify({
        lanbt_password: [
            /^[\S]{6,18}$/
            , '密码必须6到18位，且不能出现空格'
        ],
        lanbt_new_old_password: function (value, item) {
            var old_password = $('#old_password').val();
            if (value == old_password) {
                return '旧密码与新密码不得一致'
            }
        },
        lanbt_confirm_password: function (value, item) {
            var new_password = $('#new_passwrod').val();
            if (value != new_password) {
                return '两次密码不一致请重新输入'
            }
        }
    })
})